﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Security;
using System.Management.Automation.Host;

namespace Cognifide.PowerShell.Shell.Host
{
    public class ScriptingHostUserInterface : PSHostUserInterface
    {
        private ScriptingHostRawUserInterface rawUI;

        /// <summary>
        /// A reference to the PSHost implementation.
        /// </summary>
        public List<OutputLine> Output { get; private set; }

        public ScriptingHostUserInterface()
        {
            Output = new List<OutputLine>();
            rawUI = new ScriptingHostRawUserInterface();
        }

        public override string ReadLine()
        {
            throw new NotImplementedException();
        }

        public override SecureString ReadLineAsSecureString()
        {
            throw new NotImplementedException();
        }

        public override void Write(string value)
        {
            var splitter = new BufferSplitter(OutputLineType.Output, value, RawUI);
            var lastline = Output[Output.Count - 1];
            if (lastline.LineType == OutputLineType.Output && lastline.ForegroundColor == rawUI.ForegroundColor && lastline.BackgroundColor == RawUI.BackgroundColor)
            {
                lastline.Text += value;
            }
            else
            {
                Output.AddRange(splitter);
            }
        }

        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            var splitter = new BufferSplitter(OutputLineType.Output, value, RawUI.BufferSize.Width, foregroundColor, backgroundColor);
            Output.AddRange(splitter);
        }

        public override void WriteLine(string value)
        {
            var splitter = new BufferSplitter(OutputLineType.Output, value, RawUI);
            Output.AddRange(splitter);
        }

        public override void WriteErrorLine(string value)
        {
            var splitter = new BufferSplitter(OutputLineType.Error, value, RawUI.BufferSize.Width, ConsoleColor.Red, ConsoleColor.Black);
            Output.AddRange(splitter);
        }

        public override void WriteDebugLine(string message)
        {
            var splitter = new BufferSplitter(OutputLineType.Debug, message, RawUI);
            Output.AddRange(splitter);                              
        }

        public override void WriteProgress(long sourceId, ProgressRecord record)
        {
            var splitter = new BufferSplitter(OutputLineType.Progress, record.StatusDescription, RawUI);
            Output.AddRange(splitter);
        }

        public override void WriteVerboseLine(string message)
        {
            var splitter = new BufferSplitter(OutputLineType.Verbose, message, RawUI);
            Output.AddRange(splitter);
        }

        public override void WriteWarningLine(string message)
        {
            var splitter = new BufferSplitter(OutputLineType.Warning, message, RawUI.BufferSize.Width, ConsoleColor.Yellow, ConsoleColor.Black);
            Output.AddRange(splitter);
        }

        public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
        {
            if(!Sitecore.Context.IsBackgroundThread)
            {                
/*
                Sitecore.Context.ClientPage.ClientResponse.Input("Caption"
                                    , "*"
                                    , Sitecore.Configuration.Settings.ItemNameValidation
                                    , "'$Input' is not a valid name.", 255);
*/
                //args.WaitForPostBack();
            }
            throw new NotImplementedException();
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
        {
            throw new NotImplementedException();
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
        {
            throw new NotImplementedException();
        }

        public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
        {
            throw new NotImplementedException();
        }

        public override PSHostRawUserInterface RawUI
        {
            get
            {
                return rawUI;
            }
        }
    }
}
